﻿package  silin.utils
{
	import flash.display.DisplayObjectContainer;
	import com.bit101.components.Meter;
	import flash.events.*;
	import flash.utils.Timer;
	
	/**
	 * com.bit101.components.Meter, заточеный показывать fps<br>
	 * максимум берет из stage.frameRate
	 * @author silin (www.silin.fatal.ru)
	 */
	public class FPSMeter extends Meter
	{
		private var _timer:Timer = new Timer(500);
		private var _counter:int = 0;
		/**
		 * constructor
		 * @param	parent	контнейенр
		 * @param	xpos	координаты
		 * @param	ypos
		 */
		public function FPSMeter(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number =  0) 
		{
			super(parent, xpos, ypos, "fps");
			setSize(100, 50);
			if (stage)
			{
				maximum = stage.frameRate;
			}else
			{
				addEventListener(Event.ADDED_TO_STAGE, addedHandler);
			}
			
			addEventListener(Event.ENTER_FRAME, enterFrameHandler);
			_timer.addEventListener(TimerEvent.TIMER, timerHandler);
			_timer.start();
		}
		
		private function addedHandler(evnt:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, addedHandler);
			maximum = stage.frameRate;
		}
		
		private function timerHandler(evnt:TimerEvent):void 
		{
			value = 2*_counter;
			_counter = 0;
		}
		
		private function enterFrameHandler(evnt:Event):void 
		{
			_counter++;
		}
		
	}
	
}